package com.qf.cookies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cs")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1. 服务端创建Cookie 对象
        Cookie cookie = new Cookie("username","gavin");
        Cookie cookie2 = new Cookie("password","123456");
//     1.1 设置 Cookie 的访问路劲(设置好的路径之下的服务也可以查看到设置的cookie)
        cookie.setPath("/WebProject_war_exploded/get");
        cookie2.setPath("/WebProject_war_exploded");
//     1.2 设置cookie生命周期（有效期）
        cookie.setMaxAge(60*60);  // 按 秒 设置
        cookie2.setMaxAge(60*60);  // 按 秒 设置
//      2.将Cookie 相应给客户端
        resp.addCookie(cookie);
        resp.addCookie(cookie2);
    }
}
